﻿using System.Linq;
using System.Web.Mvc;
using KolhozWeb.Models;

namespace KolhozWeb.Controllers
{
    public class GameController : GameControllerBase
    {
        public ActionResult Index()
        {
            if (!TheGame.Options.IsStartAvailable)
                ModelState.AddModelError("", "Чтобы начать игру нужно минимум два игрока");

            if (ModelState.IsValid && !TheGame.IsStarted)
                TheGame.Start();

            return View(TheGame);
        }

        public ActionResult Results()
        {
            TheGame.IsStarted = false;
            return View(TheGame);
        }

        public ActionResult PersonPlus(string personId)
        {
            var player = TheGame.Players.ToList().FirstOrDefault(p => p.Id.ToString() == personId);
            TheGame.Plus(player);
            return Json(TheGame);
        }

        public ActionResult PersonMinus(string personId)
        {
            var player = TheGame.Players.ToList().FirstOrDefault(p => p.Id.ToString() == personId);
            TheGame.Minus(player);
            return Json(TheGame);
        }

        public ActionResult PersonPenalty(string personId)
        {
            var player = TheGame.Players.ToList().FirstOrDefault(p => p.Id.ToString() == personId);
            TheGame.Penalty(player);
            return Json(TheGame);
        }

        public ActionResult Reset()
        {
            TheGame.Reset();
            return RedirectToAction("Index");
        }

        public ActionResult Reverse()
        {
            TheGame.StartReversed();
            return RedirectToAction("Index");
        }
    }
}